package com.portal.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.portal.common.core.utils.DateUtils;
import com.portal.common.core.utils.StringUtils;
import com.portal.system.domain.SysAccessApp;
import com.portal.system.service.SysAccessAppService;
import com.portal.system.mapper.SysAccessAppMapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author novice
* @description 针对表【sys_access_app】的数据库操作Service实现
* @createDate 2025-01-21 23:39:50
*/
@Service
public class SysAccessAppServiceImpl extends ServiceImpl<SysAccessAppMapper, SysAccessApp>
    implements SysAccessAppService{

    @Override
    public List<SysAccessApp> selectSysAccessAppList(SysAccessApp sysAccessApp) {
        LambdaQueryWrapper<SysAccessApp> sysAccessAppLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (sysAccessApp != null) {
            if (StringUtils.isNotBlank(sysAccessApp.getAppName())){
                sysAccessAppLambdaQueryWrapper.like(SysAccessApp::getAppName, sysAccessApp.getAppName());
            }
        }
        List<SysAccessApp> sysAccessApps = baseMapper.selectList(sysAccessAppLambdaQueryWrapper);
        sysAccessApps.forEach(sysAccessApp1 -> {
            if (sysAccessApp1.getAppId().equals(0L)){
                sysAccessApp1.setEditable(false);
            }
            sysAccessApp1.setAppSecret("******");
        });
        return sysAccessApps;
    }

    @Override
    public SysAccessApp selectSysAccessAppByAppId(Long appId) {
        return baseMapper.selectById(appId);
    }

    @Override
    public int insertSysAccessApp(SysAccessApp sysAccessApp) {
        // 设置appKey和appSecret
        sysAccessApp.setAppKey(RandomUtil.randomString(12));
        sysAccessApp.setAppSecret(RandomUtil.randomString(12));
        sysAccessApp.setCreateTime(DateUtils.getNowDate());
        // 应用状态
        sysAccessApp.setStatus(0);
        return baseMapper.insert(sysAccessApp);
    }

    @Override
    public int updateSysAccessApp(SysAccessApp sysAccessApp) {
        LambdaUpdateWrapper<SysAccessApp> up = new LambdaUpdateWrapper<SysAccessApp>()
                .set(SysAccessApp::getAppName, sysAccessApp.getAppName())
                .set(SysAccessApp::getHomeAddress, sysAccessApp.getHomeAddress())
                .eq(SysAccessApp::getAppId, sysAccessApp.getAppId());
        return baseMapper.update(null,up);
    }

    @Override
    public int deleteSysAccessAppByAppIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public int deleteSysAccessAppById(Long id) {
        return baseMapper.deleteById(id);
    }
}




